#include <iostream>
#include <stdexcept>
using namespace std;

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/spdlog.h>

void rotating_log()
{
    int  max_size  = 1024 * 1024 * 10; // 10M
    int  max_files = 10;               // 10个文件
    auto logger    = spdlog::rotating_logger_mt("rotating_logger", "logs/rotating.log", max_size, max_files);
    logger->set_level(spdlog::level::trace);
    auto new_logger = logger->clone("new_logger");

    char        buffer[] = "Welcome to spdlog";
    int         value    = 123456;
    double      dvalue   = 123.4567890;
    std::string strmsg   = "Welcome to spdlog";
    bool        boolean  = false;
    char        chstr    = 'A';

    for (;;) {
        new_logger->trace("char* - {}", buffer);
        new_logger->debug("int - {}", value);
        new_logger->info("string - {}", strmsg.c_str());
        logger->warn("double - {}", dvalue);
        logger->error("bool - {}", boolean);
        logger->critical("char - {}", chstr);
    }
}

void daily_log()
{
    int  max_files = 10; // 10个文件
    auto logger    = spdlog::daily_logger_mt("daily_logger", "logs/daily.log", 0, 0, false, max_files);
    logger->set_level(spdlog::level::trace);
    logger->flush_on(spdlog::level::trace);
    auto new_logger = logger->clone("new_logger");

    for (;;) {
        new_logger->trace("welcome to spdlog");
        new_logger->debug("welcome to spdlog");
        new_logger->info("welcome to spdlog");
        logger->warn("welcome to spdlog");
        logger->error("welcome to spdlog");
        logger->critical("welcome to spdlog");
        throw std::runtime_error("Error occured!");
    }
}

void test_backtrace()
{
    std::shared_ptr<spdlog::logger> logger;
    try {
        logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
    } catch (const spdlog::spdlog_ex &ex) {
        std::cout << "Log init failed: " << ex.what() << std::endl;
    }

    logger->enable_backtrace(32);
    for (int i = 0; i < 100; i++) {
        logger->debug("backtrace message {}", i);
    }
    logger->dump_backtrace();
}

int main(int argc, char *argv[])
{
    // rotating_log();
    daily_log();
    // test_backtrace();
    return 0;
}